package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.authority.UsedPartyDetail;
import com.example.mapp.UsedPartyDetailMapp;
import com.example.service.UsedPartyDetaiService;
import com.example.util.ResultUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Map;

/**
 * Created by simon on 2017/10/18.
 */
@Service
public class UsedPartyDetailServiceImpl implements UsedPartyDetaiService {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(UsedPartyDetailServiceImpl.class);

    @Autowired
    UsedPartyDetailMapp usedPartyDetailMapp;

//    @Autowired
//	PromotionMapp promotionMapp;

    @Override
    public Map<String, Object> queryById(String partyId) {
        UsedPartyDetail usedPartyDetail = null;
        try{
            usedPartyDetail = usedPartyDetailMapp.queryById(partyId);
        }catch (Exception e){
            logger.error("查询失败",e);
            return ResultUtil.returnError("查询失败",null);
        }
        return ResultUtil.returnSuccess("查询成功", usedPartyDetail);
    }

    @Override
    @Transactional
    public Map<String, Object> add(UsedPartyDetail usedPartyDetail) {
        try{
              usedPartyDetailMapp.add(usedPartyDetail);
//            if (null != usedPartyDetail.getZzjb()) {
//            	PromotionParty promotionParty = promotionMapp.findPartyById(usedPartyDetail.getPartyId());
//        		promotionParty.setZzjb(usedPartyDetail.getZzjb());
//        		promotionMapp.updateParty(promotionParty);
//            	if ("一".equalsIgnoreCase(usedPartyDetail.getZzjb())) {
//            		PromotionUtil.calcParty(promotionMapp, 2000, "2", usedPartyDetail.getPartyId());
//            		PromotionRecord promotionRecord1 = new PromotionRecord();
//					promotionRecord1.setUserId(String.valueOf(usedPartyDetail.getPartyId()));
//					promotionRecord1.setType("2");
//					promotionRecord1.setEmpiricItem("一级资质");
//					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//					promotionRecord1.setEmpiricValue(2000);
//					promotionMapp.insertRecord(promotionRecord1);
//            	} else if ("二".equalsIgnoreCase(usedPartyDetail.getZzjb())) {
//            		PromotionUtil.calcParty(promotionMapp, 1500, "2", usedPartyDetail.getPartyId());
//            		PromotionRecord promotionRecord1 = new PromotionRecord();
//					promotionRecord1.setUserId(String.valueOf(usedPartyDetail.getPartyId()));
//					promotionRecord1.setType("2");
//					promotionRecord1.setEmpiricItem("二级资质");
//					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//					promotionRecord1.setEmpiricValue(1500);
//					promotionMapp.insertRecord(promotionRecord1);
//            	} else if ("三".equalsIgnoreCase(usedPartyDetail.getZzjb())) {
//            		PromotionUtil.calcParty(promotionMapp, 1000, "2", usedPartyDetail.getPartyId());
//            		PromotionRecord promotionRecord1 = new PromotionRecord();
//					promotionRecord1.setUserId(String.valueOf(usedPartyDetail.getPartyId()));
//					promotionRecord1.setType("2");
//					promotionRecord1.setEmpiricItem("三级资质");
//					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//					promotionRecord1.setEmpiricValue(1000);
//					promotionMapp.insertRecord(promotionRecord1);
//            	}
//            }
        }catch (Exception e){
        	TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error("新增失败",e);
            ResultUtil.returnError("新增失败",null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }

    @Override
    @Transactional
    public Map<String, Object> update(UsedPartyDetail usedPartyDetail) {
        try{
              usedPartyDetailMapp.update(usedPartyDetail);
//            PromotionParty promotionParty = promotionMapp.findPartyById(usedPartyDetail.getPartyId());
//            String zzjb = promotionParty.getZzjb();
//            if (null != usedPartyDetail.getZzjb()) {
//        		promotionParty.setZzjb(usedPartyDetail.getZzjb());
//        		promotionMapp.updateParty(promotionParty);
//            	if ("一".equalsIgnoreCase(usedPartyDetail.getZzjb())) {
//            		if ("二".equalsIgnoreCase(zzjb)) {
//            			PromotionUtil.calcParty(promotionMapp, 500, "2", usedPartyDetail.getPartyId());
//            			PromotionRecord promotionRecord1 = new PromotionRecord();
//    					promotionRecord1.setUserId(String.valueOf(usedPartyDetail.getPartyId()));
//    					promotionRecord1.setType("2");
//    					promotionRecord1.setEmpiricItem("二级资质转一级资质");
//    					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//    					promotionRecord1.setEmpiricValue(500);
//    					promotionMapp.insertRecord(promotionRecord1);
//            		} else if ("三".equalsIgnoreCase(zzjb)) {
//            			PromotionUtil.calcParty(promotionMapp, 1000, "2", usedPartyDetail.getPartyId());
//            			PromotionRecord promotionRecord1 = new PromotionRecord();
//    					promotionRecord1.setUserId(String.valueOf(usedPartyDetail.getPartyId()));
//    					promotionRecord1.setType("2");
//    					promotionRecord1.setEmpiricItem("三级资质转一级资质");
//    					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//    					promotionRecord1.setEmpiricValue(1000);
//    					promotionMapp.insertRecord(promotionRecord1);
//            		}
//            	} else if ("二".equalsIgnoreCase(usedPartyDetail.getZzjb())) {
//            		if ("一".equalsIgnoreCase(zzjb)) {
//            			PromotionUtil.calcParty(promotionMapp, -500, "2", usedPartyDetail.getPartyId());
//            			PromotionRecord promotionRecord1 = new PromotionRecord();
//    					promotionRecord1.setUserId(String.valueOf(usedPartyDetail.getPartyId()));
//    					promotionRecord1.setType("2");
//    					promotionRecord1.setEmpiricItem("一级资质转二级资质");
//    					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//    					promotionRecord1.setEmpiricValue(-500);
//    					promotionMapp.insertRecord(promotionRecord1);
//            		} else if ("三".equalsIgnoreCase(zzjb)) {
//            			PromotionUtil.calcParty(promotionMapp, 500, "2", usedPartyDetail.getPartyId());
//            			PromotionRecord promotionRecord1 = new PromotionRecord();
//    					promotionRecord1.setUserId(String.valueOf(usedPartyDetail.getPartyId()));
//    					promotionRecord1.setType("2");
//    					promotionRecord1.setEmpiricItem("三级资质转二级资质");
//    					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//    					promotionRecord1.setEmpiricValue(500);
//    					promotionMapp.insertRecord(promotionRecord1);
//            		}
//            	} else if ("三".equalsIgnoreCase(usedPartyDetail.getZzjb())) {
//            		if ("一".equalsIgnoreCase(zzjb)) {
//            			PromotionUtil.calcParty(promotionMapp, -1000, "2", usedPartyDetail.getPartyId());
//            			PromotionRecord promotionRecord1 = new PromotionRecord();
//    					promotionRecord1.setUserId(String.valueOf(usedPartyDetail.getPartyId()));
//    					promotionRecord1.setType("2");
//    					promotionRecord1.setEmpiricItem("一级资质转三级资质");
//    					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//    					promotionRecord1.setEmpiricValue(-1000);
//    					promotionMapp.insertRecord(promotionRecord1);
//            		} else if ("二".equalsIgnoreCase(zzjb)) {
//            			PromotionUtil.calcParty(promotionMapp, -500, "2", usedPartyDetail.getPartyId());
//            			PromotionRecord promotionRecord1 = new PromotionRecord();
//    					promotionRecord1.setUserId(String.valueOf(usedPartyDetail.getPartyId()));
//    					promotionRecord1.setType("2");
//    					promotionRecord1.setEmpiricItem("二级资质转三级资质");
//    					promotionRecord1.setAddedTime(PromotionUtil.getTime());
//    					promotionRecord1.setEmpiricValue(-500);
//    					promotionMapp.insertRecord(promotionRecord1);
//            		}
//            	}
//            }
        }catch (Exception e){
        	TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error("失败",e);
            ResultUtil.returnError("失败",null);
        }
        return ResultUtil.returnSuccess("成功",null);
    }
}
